import json

from django.db.models import Q
from django.shortcuts import *

# Create your views here.
from web_stu.models import *
from django.http import *
from web_stu.fenye import *

def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    username = request.POST.get('username')
    password = request.POST.get('password')
    print(username)
    userinfo = Admi.objects.filter(username=username,password=password).first()
    if userinfo:
        request.session['user'] = userinfo.username
        request.session.set_expiry(0)

        return JsonResponse({'code':200,'html':'index'})
    return JsonResponse({'code':400,'msg':'帐号密码错误'})



def index(request):
    if request.method == 'GET':
        if request.session.get('user'):
            stuinfo = Stu.objects.all()
            page_object = Pagination(
                current_page=request.GET.get('page'),
                all_count=stuinfo.count(),
                base_url=request.path_info,
                query_params=request.GET
            )
            issues_object_list = stuinfo[page_object.start:page_object.end]
            context = {
                'issues_object_list': issues_object_list,
                'page_html': page_object.page_html()
            }
            return render(request,'index.html',context)
        return redirect(reverse('login'))


def Addstu(request):
    if request.method == 'GET':
        if request.session.get('user'):
            return render(request,'add.html')
        return redirect(reverse('login'))
    name = request.POST.get('name')
    sex = request.POST.get('sex')
    xueyaun = request.POST.get('xueyaun')
    clas = request.POST.get('clas')
    zhiwu = request.POST.get('zhiwu')
    tpl = request.POST.get('tpl')
    b_time = request.POST.get('b_time')
    Stu.objects.create(name=name,sex=sex,xueyaun=xueyaun,clas=clas,zhiwu=zhiwu,tpl=tpl,b_time=b_time)
    return redirect(reverse('index'))
def Upstu(request,id):
    if request.method == 'GET':
        if request.session.get('user'):
            stu_info=Stu.objects.filter(id=id).first()
            return render(request,'up.html',{'stu_info':stu_info})
        return redirect(reverse('login'))
    name = request.POST.get('name')
    sex = request.POST.get('sex')
    xueyaun = request.POST.get('xueyaun')
    clas = request.POST.get('clas')
    zhiwu = request.POST.get('zhiwu')
    tpl = request.POST.get('tpl')
    b_time = request.POST.get('b_time')
    Stu.objects.filter(id=id).update(name=name,sex=sex,xueyaun=xueyaun,clas=clas,zhiwu=zhiwu,tpl=tpl,b_time=b_time)
    return redirect(reverse('index'))

def delete(request,id):
    if request.method == 'GET':
        if request.session.get('user'):
            Stu.objects.filter(pk=id).delete()
            return redirect(reverse('index'))
        return redirect(reverse('login'))
def selet(request):
    name = request.POST.get('name')
    # name = json.loads(request.body.decode())
    # print(name.name)
    stuinfo = Stu.objects.filter(Q(name=name)|Q(xueyaun=name)|Q(clas=name))
    print(stuinfo)
    list = []
    for i in stuinfo:
        list.append({
            'id':i.id,
            'name': i.name,
            'sex': i.sex,
            'xueyaun': i.xueyaun,
            'clas': i.clas,
            'zhiwu': i.zhiwu,
            'tpl': i.tpl,
            'b_time': i.b_time,
        })
    return JsonResponse({'code':200,'lists':list})